"""
app main
Author: kaspar
Date: 2022-03-29
"""
import tornado
import multiprocessing
from tornado.web import Application
from tornado.ioloop import IOLoop
from tornado.httpserver import HTTPServer
from util.logger import Logger
from util.config import Config
from route import route
from handler import handler_schedule
from tornado.options import define

DEFINE_ROLE = 'slave' # role master slave

def server_worker():
    """
    server worker
    """
    Logger.writelog("info", "start server")
    # opera dynamic port get
    default_port = Config.get_conf('server', 'port')
    app = Application(route.route)
    http_server = HTTPServer(app)
    http_server.bind(default_port)
    http_server.start(1)
    IOLoop.current().start()

def main():
    server_process = multiprocessing.Process(name="server", target=server_worker)
    schedule = None
    if DEFINE_ROLE == 'slave':
        schedule = multiprocessing.Process(name='schedule', target=handler_schedule.schedule_job)

    server_process.start()
    if schedule:
        schedule.start()
    server_process.join()
    if schedule:
        schedule.join()

if __name__ == "__main__":
    """
    main func
    """
    main()